<!-- 用餐记录 -->

<template>
  <div>
    <el-row>
      <el-col :span="5">
        <div style="width: 100%">
          <user-list
              @handleSelectionChange="handleSelectionChange"
              stage="ing"
              businessType="lifeCare"
              ref="userList"
              @addClick="addClick"
          ></user-list>
        </div>
      </el-col>
      <el-col :span="19">
        <div style="margin-left: 20px">
          <navigation-bar customTitle="生活护理记录">
            <div class="searchContent">
              <el-input
                  v-model="examineeName"
                  placeholder="请输入姓名"
                  size="mini"
                  style="width: 150px; margin-right: 10px"
              ></el-input>
              <!--							<label style="font-size: 14px">护理类型：</label>-->
              <el-select
                  clearable
                  v-model="typeValue"
                  placeholder="请选择护理类型"
                  size="mini"
                  style="width: 150px; margin-right: 10px"
                  @change="typeChange"
              >
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    width="150px"
                >
                </el-option>
              </el-select>
              <!-- <el-select
                v-model="zqh"
                clearable
                placeholder="请选择周期"
                size="mini"
                style="width: 150px; margin-right: 10px"
              >
                <el-option
                  v-for="item in zqhList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  width="150px"
                >
                    </el-option>
                  </el-select> -->
              <custom-button
                  type="search"
                  style="margin-right: 10px"
                  @searchClick="searchClick"
              ></custom-button>
              <custom-button
                  type="reset"
                  style="margin-right: 10px"
                  @resetClick="resetClick"
              ></custom-button>
              <custom-button
                  type="add"
                  @addClick="addClick"
                  style="margin-right: 10px"
              ></custom-button>
              <!-- <custom-button
                type="add"
                @addClick="addClick"
                style="margin-right: 10px"
                    title="批量新增"
                  ></custom-button> -->
            </div>
          </navigation-bar>
          <table-index
              :dataSource="dataSource"
              :tableColumns="tableColumns"
              :pageNo="pageNo"
              :pageCount="pageCount"
              :pageSize="pageSize"
              :btnList="btnList"
              @pageChange="pageChange"
              @editRow="editRow"
              @viewRow="viewRow"
              @deleteRow="deleteRow"
          ></table-index>
        </div>
      </el-col>
    </el-row>

    <div v-if="dialogShow">
      <custom-dialog
          :title="dialogTitle"
          size="large"
          @dialogClose="dialogClose"
      >
        <form-index :formConfig="formConfig" @submit="submit" :type="type">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
  </div>
</template>
<script>
import {allForm} from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/careForm/allForm"
import {toiletForm} from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/careForm/toiletForm"
import {trocarPositionForm} from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/careForm/trocarPositionForm"
import {waterForm} from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/careForm/waterForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import userList from "@/components/customComponents/managementCenter/userList"
import {AjaxCommon} from "@/libs/apiCommon"
import {PopupCommon} from "@/libs/popupCommon"
import {SessionCommon} from "@/libs/sessionCommon"
import {Utility} from "@/libs/utility"

export default {
  components: {
    tableIndex,
    userList,
    navigationBar,
    customButton,
    customDialog,
    formIndex,
  },
  data() {
    return {
      type: "add",
      btnList: ["edit", "view", "delete"],
      leftSelectIds: [],
      dialogTitle: "测试",
      // form的配置信息
      formConfig: toiletForm,
      dialogShow: false,
      examineeName: "",
      // 左边table相关参数 begin
      dataSource: [],
      tableColumns: [],
      tableColumnsAll: [
        {
          prop: "randomNo",
          label: "随机号",
          width: 70,
        },
        {
          prop: "groupSeq",
          label: "入组顺序号",
          width: 100,
        },
        {
          prop: "drugSeq",
          label: "给药序列号",
          width: 100,
        },
        {
          prop: "examineeName",
          label: "受试者名称",
          width: 100,
        },
        {
          prop: "sex",
          label: "性别",
          width: 70,
          dictName: "sex",
        },
        {
          prop: "careType",
          label: "护理类型",
          dictName: "life_care_type",
          width: 100,
        },
        {
          prop: "careTime",
          label: "护理时间",
          width: 160,
        },
        {
          prop: "remarks",
          label: "护理记录内容",
          width: 240,
        },
        {
          prop: "createByName",
          label: "记录人",
        },
      ],
      pageNo: 1,
      pageCount: 0,
      pageSize: 20,
      // 左边table相关参数 end
      input: "",
      options: Utility.getDictList("life_care_type")
      //   .filter(
      // (t) => t.label == "进水" || t.label == "留置针" || t.label == "如厕")
      ,
      typeValue: "",
      typeName: "",
      examineeCodeList: [],
    }
  },
  methods: {
    typeChange(e) {
      this.typeName = this.options.find((t) => t.value == e).label
      this.typeValue = e
      if (this.typeName == "进水") {
        this.formConfig = waterForm
      } else if (this.typeName == "如厕") {
        this.formConfig = toiletForm
      } else if (this.typeName == "留置针") {
        this.formConfig = trocarPositionForm
      } else {
        this.formConfig = allForm
      }
    },
    printClick() {
    },
    submit(d) {
      let self = this
      if (d.id) {
        AjaxCommon("/ccdc/pm/em/ing/emIngLifeCare/edit", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                this.$refs.userList.refresh()
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      } else {
        d.examineeCode = self.leftSelectIds[0].examineeCode
        d.examineeEn = self.leftSelectIds[0].examineeEn
        d.examineeName = self.leftSelectIds[0].examineeName
        d.filterNo = self.leftSelectIds[0].filterNo
        d.groupSeq = self.leftSelectIds[0].groupSeq
        d.randomNo = self.leftSelectIds[0].randomNo
        d.drugSeq = self.leftSelectIds[0].drugSeq
        d.sex = self.leftSelectIds[0].sex
        d.careType = self.typeValue
        d.careTime = Utility.getCurrentTime()
        delete d.id
        AjaxCommon("/ccdc/pm/em/ing/emIngLifeCare/add", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                this.$refs.userList.refresh()
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      }
    },
    dialogClose() {
      this.dialogShow = false
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.examineeName = ""
      this.typeValue = ""
      this.typeName = ""
      this.tableColumns = this.tableColumnsAll
    },
    addClick() {
      if (this.leftSelectIds.length != 1) {
        PopupCommon.notify("info", "请勾选一个受试者")
        return
      }
      if (this.typeValue == "") {
        PopupCommon.notify("info", "请选择护理类型")
        return
      }

      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            if (t[res] == "examineeCode") {
              t.filedValue = this.leftSelectIds[0].examineeCode
            } else if (t[res] == "examineeName") {
              t.filedValue = this.leftSelectIds[0].examineeName
            } else if (t[res] == "randomNo") {
              t.filedValue = this.leftSelectIds[0].randomNo
            } else if (t[res] == "groupSeq") {
              t.filedValue = this.leftSelectIds[0].groupSeq
            } else {
              t.filedValue = ""
            }
          }
        })
      })
      formConfigItem.groupId = this.leftSelectIds[0].id
      this.formConfig = formConfigItem

      this.type = "add"
      this.dialogShow = true
      this.dialogTitle =
          "新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    editRow(d) {
      if ("jinshui" === d.data.careType) {
        this.formConfig = waterForm
      } else if ("ruce" === d.data.careType) {
        this.formConfig = toiletForm
      } else if ("liuzhizhen" === d.data.careType) {
        this.formConfig = trocarPositionForm
      } else {
        this.formConfig = allForm
      }
      this.type = "edit"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            if (t[res] == "time"){
              t.filedValue = [d.data.joinTime, d.data.outTime]
            }else{
              t.filedValue = d.data[t[res]]
            }
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
          "编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    viewRow(d) {
      if ("jinshui" === d.data.careType) {
        this.formConfig = waterForm
      } else if ("ruce" === d.data.careType) {
        this.formConfig = toiletForm
      } else if ("liuzhizhen" === d.data.careType) {
        this.formConfig = trocarPositionForm
      } else {
        this.formConfig = allForm
      }
      this.type = "look"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            if (t[res] == "time"){
              t.filedValue = [d.data.joinTime, d.data.outTime]
            }else{
              t.filedValue = d.data[t[res]]
            }
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
          "查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    deleteRow(d) {
      const id = d.data.id
      let self = this
      AjaxCommon("/ccdc/pm/em/ing/emIngLifeCare/delete?id=" + id, "delete", {
        optReason: d.optReason,
      }).then((res) => {
        console.log(res, "res...............")
        if (res.data.code == 200) {
          PopupCommon.notify("success", "删除成功")
          this.$refs.userList.refresh()
          self.initPage()
          self.dialogClose()
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
    // 左侧选中事件
    handleSelectionChange(d) {
      console.log(d)
      this.leftSelectIds = d
      this.examineeCodeList = d ? d.map((t) => t.examineeCode) : []
      this.initPage()
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon("/ccdc/pm/em/ing/emIngLifeCare/listByPage", "post", {
        examineeName: this.examineeName,
        careType: this.typeValue,
        examineeCodeList: this.examineeCodeList,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.tableColumns = this.tableColumnsAll
    this.initPage()
  },
}
</script>

<style></style>
